package nl.cmkprojects.sage.slimplayer;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.PatternSyntaxException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

@XmlRootElement
@XmlType
public class ArtistAliasList {
  private static Logger logger = Logger.getLogger(ArtistAliasList.class);

  @XmlElement(name = "artistAlias")
  protected List<ArtistAlias> artistAliasList = new ArrayList<ArtistAlias>();

  public ArtistAlias getAlias(String artist, String title) {
    for (ArtistAlias alias : artistAliasList) {
      // artist match
      if ((StringUtils.isNotEmpty(artist) && StringUtils.isNotEmpty(alias.getArtist()))
          && (StringUtils.isEmpty(title) || StringUtils.isEmpty(alias.getTitle()))) {
        if (match(artist, (String) alias.getArtist())) {
          return alias;
        }
      }
      // title match
      if ((StringUtils.isNotEmpty(title) && StringUtils.isNotEmpty(alias.getTitle()))
          && (StringUtils.isEmpty(artist) || StringUtils.isEmpty(alias.getArtist()))) {
        if (match(title, (String) alias.getTitle())) {
          return alias;
        }
      }
      // artist + title match
      if (StringUtils.isNotEmpty(title) && StringUtils.isNotEmpty(alias.getTitle()) && StringUtils.isNotEmpty(artist)
          && StringUtils.isNotEmpty(alias.getArtist())) {
        if (match(title, (String) alias.getTitle()) && match(artist, (String) alias.getArtist())) {
          return alias;
        }
      }
    }
    return null;
  }

  public List<ArtistAlias> getArtistAliasList() {
    return artistAliasList;
  }

  private boolean match(String value, String pattern) {
    try {
      return value.matches(pattern);
    } catch (PatternSyntaxException e) {
      logger.error("pattern=" + pattern, e);
    }
    return false;
  }

  public static void storeInstance(ArtistAliasList list) {
    try {
      JAXBContext context = JAXBContext.newInstance(ArtistAliasList.class);
      Marshaller marshaller = context.createMarshaller();
      marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
      marshaller.marshal(list, System.out);
    } catch (Exception e) {
      logger.error(e);
    }
  }

  public static ArtistAliasList getInstance() {
    try {
      return getInstance(new FileInputStream(new File("slimplayer/slimplayer-ai-alias.xml")));
    } catch (Exception e) {
      logger.error(e);
    }
    return new ArtistAliasList();
  }

  public static ArtistAliasList getInstance(InputStream input) {
    try {
      JAXBContext context = JAXBContext.newInstance(ArtistAliasList.class);
      Unmarshaller unmarshaller = context.createUnmarshaller();
      return (ArtistAliasList) unmarshaller.unmarshal(input);
    } catch (Exception e) {
      logger.error(e);
    }
    return new ArtistAliasList();
  }
}
